﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AiChiTu.TraceabilitySystem.ErrorCode.Extensions
{
	/// <summary>
	/// 枚举类型数据的扩展方法
	/// </summary>
	public static class EnumsHelper
	{
		/// <summary>
		/// 获取枚举值对应的自定义特性描述
		/// </summary>
		/// <param name="value">枚举值</param>
		/// <returns>返回字符串</returns>
		public static string GetDescription(this Enum value)
		{
			// 根据枚举值 获取枚举值的字段名
			FieldInfo? fieldInfo = value.GetType().GetField(value.ToString());
			// 根据字段名 获取该字段的自定义特性描述
			DescriptionAttribute? descriptionAttribute = fieldInfo?.GetCustomAttribute<DescriptionAttribute>();
			return descriptionAttribute != null ? descriptionAttribute.Description : value.ToString();
		}
	}
}
